package com.lst.projectlib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.lst.projectlib.util.StringUtil;

/**
 * Created by Administrator on 2017/3/2.
 */

public class SystemUtil {
    /**
     * 获取设备硬件的唯一序列编码
     * @return
     */
    public static String getDeviceUniqueCode(Context context) {
        String device_id = "";
        String android_id = "";
        String serial = "";

        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            // 获取device_id
            device_id = tm.getDeviceId();
        } catch (Exception e) {
        }

        try {
            // 获取android_id
            android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
        }

        try {
            // 获取手机序列号
            serial = android.os.Build.SERIAL;
        } catch (Exception e) {
        }

        // 开始通过MD5算法计算
        String code = "";
        if (!TextUtils.isEmpty(device_id)) {
            code += device_id;
        }
        if (!TextUtils.isEmpty(android_id)) {
            code += android_id;
        }
        if (!TextUtils.isEmpty(serial)) {
            code += serial;
        }

        String str_md5 = StringUtil.stringToMD5(code);
        if (!TextUtils.isEmpty(str_md5)) {
            return str_md5;
        } else {
            try {
                // 模拟计算一个唯一标示
                code = "dong" + android.os.Build.BOARD.length()%10 +
                        android.os.Build.BRAND.length()%10 +
                        android.os.Build.DEVICE.length()%10 +
                        android.os.Build.DISPLAY.length()%10 +
                        android.os.Build.HOST.length()%10 +
                        android.os.Build.ID.length()%10 +
                        android.os.Build.MANUFACTURER.length()%10 +
                        android.os.Build.MODEL.length()%10 +
                        android.os.Build.PRODUCT.length()%10 +
                        android.os.Build.TAGS.length()%10 +
                        android.os.Build.TYPE.length()%10 +
                        android.os.Build.USER.length()%10;
            } catch (Exception e) {
            }
            // 截取16位
            if (!TextUtils.isEmpty(code) && code.length() > 16) {
                code = code.substring(0, 16);
            }
            return code;
        }
    }

    /**
     * 获取状态栏的高度
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context){
        int statusHeight = 0;
        Rect localRect = new Rect();
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight){
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
                statusHeight = context.getResources().getDimensionPixelSize(i5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

}
